// pages/transfer/transfer.js
var util = require('../../utils/util.js');
const app = getApp();
var pageNo = 1;
var hasNextPage = false;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('view.open-type.getUserInfo'),
    baseDomain: app.globalData.baseDomain,
    apiDomain: app.globalData.apiDomain,
    transfer: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    //调用获取帮转列表接口
    wx.request({
      url: app.globalData.apiDomain + '/api/transfer/getTransfer/1',
      success: function(res) {
        hasNextPage = res.data.data.hasNextPage;
        that.setData({
          transfer: res.data.data.list,
        });
      },
      complete: function() {
        if (hasNextPage) {
          pageNo = 2;
        }
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    var that = this;
    //调用获取帮转列表接口
    wx.request({
      url: app.globalData.apiDomain + '/api/transfer/getTransfer/1',
      success: function(res) {
        hasNextPage = res.data.data.hasNextPage;
        that.setData({
          transfer: res.data.data.list,
        });
      },
      complete: function() {
        if (hasNextPage) {
          pageNo = 2;
        }
        wx.stopPullDownRefresh();
      }
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this;
    //调用获取帮转列表接口
    if (hasNextPage) {
      wx.request({
        url: app.globalData.apiDomain + '/api/transfer/getTransfer/' + pageNo,
        success: function(res) {
          hasNextPage = res.data.data.hasNextPage;
          var oldTransfer = that.data.transfer;
          var list = res.data.data.list;
          for (var i = 0; i < list.length; i++) {
            oldTransfer.push(list[i]);
          }
          that.setData({
            transfer: oldTransfer,
          });
        },
        complete: function() {
          if (hasNextPage) {
            pageNo = 2;
          }
        }
      });
    }

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  navigateToDetail: util.throttle(function(e) {
    if (e.currentTarget.dataset.type == 1) {
      wx.navigateTo({
        url: '../transferDetail/transferDetail?transferId=' + e.currentTarget.dataset.transferid,
      })
    } else if (e.currentTarget.dataset.type == 2) {
      wx.navigateTo({
        url: '../detail/detail?goodsId=' + e.currentTarget.dataset.goodsid,
      })
    }
  }),
  //跳转至添加页面
  navigateToAdd: util.throttle(function(e) {
    if (app.globalData.userInfo) {
      wx.navigateTo({
        url: '../add-transfer/add-transfer',
      })
    } else {
      wx.switchTab({
        url: '../userCenter/userCenter',
        complete: function() {
          wx.showToast({
            title: '请先登录',
            image: '../../images/failure_toast.png',
            duration: 2000,
          });
        }
      })
    }
  })
})